package com.mymoney.persistence.managers;

import java.sql.Timestamp;
import java.util.List;

import com.mymoney.beans.ItemBean;
import com.mymoney.exceptions.MyMoneyException;
import com.mymoney.persistence.PersistenceContext;


public class ItemManager extends AbstractBeanManager<ItemBean> {

	public ItemManager(PersistenceContext persistenceContext) {
		super(persistenceContext);
	}


	@Override
	public List<ItemBean> list() throws MyMoneyException {
		
		String queryString = "select d from ItemBean d";
		List<ItemBean> items = findAllEntities(queryString, -1,(Object[]) null);
		return items;
	}
	
	public List<ItemBean> listPlanned(String primaryAccount) throws MyMoneyException {
		
		String queryString = "select d from ItemBean d where d.isBought = 0 and d.primaryAccount = :primaryAccount";
		List<ItemBean> items = findAllEntities(queryString, -1, "primaryAccount", primaryAccount);
		return items;
	}
	
	public List<ItemBean> listBought(String primaryAccount) throws MyMoneyException {
		
		String queryString = "select d from ItemBean d where d.isBought <> 0 and d.primaryAccount = :primaryAccount";
		List<ItemBean> items = findAllEntities(queryString, -1, "primaryAccount", primaryAccount);
		return items;
	}


	@Override
	public ItemBean getByCode(long code) throws MyMoneyException {
		
		return getBy("select p from ItemBean p where p.code = :code", code, "code");
	}


	@Override
	public ItemBean getById(Integer id) throws MyMoneyException {

		return getBy("select p from ItemBean p where p.id = :id", id, "id");
	}


	@Override
	public ItemBean getByName(String name) throws MyMoneyException {

		return getBy("select p from ItemBean p where p.name = :name", name, "name");
	}


	@Override
	public void update(ItemBean item, ItemBean existingItem) throws MyMoneyException {
		
		if(existingItem != null)
		{
			//NAME
			if (item.getName() != null) {
				existingItem.setName(item.getName());
			}
			
			//STORE
			existingItem.setStore(item.getStore());
			
			//CATEGORY
			if (item.getCategory() != null) {
				existingItem.setCategory(item.getCategory());
			}
			
			//PRICE
			existingItem.setPrice(item.getPrice());
			
			//PLAN DATE
			if(item.getPlanDate() != null)
			{
				existingItem.setPlanDate(item.getPlanDate());
			}
			
			//BUY DATE
			existingItem.setBuyDate(new Timestamp(System.currentTimeMillis()));
			
			//CURRENCY
			if (item.getCurrency() != null) {
				existingItem.setCurrency(item.getCurrency());
			}
			
			//IS BOUGHT
			existingItem.setBought(item.isBought());

			//PRIMARY ACCOUNT
			if(item.getPrimaryAccount() != null)
			{
				existingItem.setPrimaryAccount(item.getPrimaryAccount());
			}
			
			
			
		}
		else
		{
			throw new MyMoneyException("Item is not initialized.");
		}
		
	}


	@Override
	public void update(ItemBean item, long code) throws MyMoneyException {
		
		ItemBean existingItem = getByCode(code);
		update(item, existingItem);
		
	}


	@Override
	public void update(ItemBean item, Integer id) throws MyMoneyException {
		
		ItemBean existingItem = getById(id);
		update(item, existingItem);
		
	}


	@Override
	public void update(ItemBean entity, String name) throws MyMoneyException {
		throw new MyMoneyException("Method is not implemented!");
		
	}


	@Override
	public void add(ItemBean item) throws MyMoneyException {
		
		if(item != null)
		{
			item.setPlanDate(new Timestamp(System.currentTimeMillis()));
			item.setCode(System.currentTimeMillis());
			storeEntity(item);
		}
		else
		{
			throw new MyMoneyException("Item is not defined.");
		}
	}
}
